import Vue from "vue";
import VueRouter from "vue-router";
import routes from "./router"
import state from "@/store/index"
import store from "@/store/index";
Vue.use(VueRouter)
let originPush=VueRouter.prototype.push;
VueRouter.prototype.push=function(location,resolve,reject){
     if(resolve&&reject){
       originPush.call(this,location,resolve.reject)
       
     }else{
       originPush.call(this,location,()=>{},()=>{})
     }
}
 let router=new VueRouter({
  routes,
  scrollBehavior(to,from,savedPosition){
    return {y:0}
  }
})
router.beforeEach(async (to,form,next)=>{
  if(localStorage.getItem("TOKEN")){
    if(to.path=="/login"||to.path=="/register"){
      next("/home")
    }else{
      next()
    }
  }else{
     let toPach=to.path
     if(toPach.indexOf("/trade")!=-1||toPach.indexOf("/pay")!=-1||toPach.indexOf("/center")!=-1){
       next("/login?redirect="+toPach)
     }else{
       next()
     }
  }
})
export default router